# -*- coding:utf-8 -*-
__doc__ = '我的已审'

from PageClass import EcsIndexPage, EcsMyApprovedPage
from TestCases.common._baseClass import _BaseClass
from Util import logger, ZTCCLOUDError, config


class BusinessApproved(_BaseClass):

    def __init__(self, driver, isExit: bool = False):
        self.driver = driver
        self.isExit = isExit
        self.__goToMyApproved(self.driver)
        self._ecsMyApprovedPage = EcsMyApprovedPage(self.driver)

    def __exit__(self, *args):
        if self.isExit:
            self.exit()

    def exit(self):
        self.driver.quit()

    def __goToMyApproved(self, driver):
        self._ecsIndexPage = EcsIndexPage(driver)
        self._ecsIndexPage.driver.get(config.urlDict.url.ecsHost)
        self._ecsIndexPage.click_myApproved()

    def __findBoe(self, boeNo):
        self._ecsMyApprovedPage.input_boeNo(boeNo)
        self._ecsMyApprovedPage.click_boeNoSelectButton()

        status = self._ecsMyApprovedPage.selectResultIsOrNot(boeNo)
        if not status:
            raise ZTCCLOUDError(f"{boeNo} do not exist")

    def __isOrNotSuccess(self):
        if '成功' in self._ecsMyApprovedPage.getBoxMessage2():
            content = '撤回成功'
            logger.info(f"审批状态为：{content}")
            return content
        else:
            content = '撤回失败'
            logger.info(f"审批状态为：{content}")
            return content

    # 撤回
    def boeBusinessApproveWithdraw(self, boeNo):
        try:
            self.__findBoe(boeNo)
            self._ecsMyApprovedPage.click_withdrawButton()
            self._ecsMyApprovedPage.click_boeBusinessTipConfirm()
            return self.__isOrNotSuccess()
        except Exception as e:
            logger.error(f"我的已审撤回出现异常，异常信息为：{str(e)}")
